#include "MaterialLoader.h"

Material * MaterialLoader::LoadFromStream(std::istream &bs, TexturesManager *texMan)
{
	Material *mat = new Material();

	bs.read((char*)&mat ->ambient, sizeof(sm::Vec3));
	bs.read((char*)&mat ->diffuse, sizeof(sm::Vec3));
	bs.read((char*)&mat ->specular, sizeof(sm::Vec3));

	bs.read((char*)&mat ->specularLevel, sizeof(float));
	bs.read((char*)&mat ->glossiness, sizeof(float));
	bs.read((char*)&mat ->opacity, sizeof(float));

	BYTE hasTex;
	bs.read((char*)&hasTex, sizeof(BYTE));

	if (hasTex == 1)
	{
		BYTE texNameSize;
		char *texName;

		bs.read((char*)&texNameSize, sizeof(BYTE));
		texName = new char[texNameSize + 1];
		texName[texNameSize] = 0;
		bs.read(texName, texNameSize);
		texName[strlen(texName) - 4] = 0; // memory leak

		mat ->texture = texMan ->Get(texName);
	}
	else
		mat ->texture = NULL;

	return mat;
}
